home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / bit2spr / bit2spr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-22  |  3.7 KB  |  166 lines

  1. /*
  2.  * Bitmap to LaTeX Sprite 
  3.  * by Marc David Rovner (mrovner@ic.sunysb.edu)
  4.  * June 7th, 1992
  5.  */
  6.  
  7. #include    <stdio.h>
  8. #include    <stdlib.h>
  9.  
  10. #define MAXFILENAME    1024
  11. #define INITWIDTH    1.5
  12. #define INITHEIGHT    1.5
  13.  
  14. typedef struct {
  15.     char OutFileName[MAXFILENAME];
  16.     float WidthRatio, HeightRatio;
  17. } ConvInfo;
  18.  
  19. void conv_bitmap(Stuff,bitmapfile,spritefile,spritename)
  20. ConvInfo Stuff;
  21. FILE *bitmapfile, *spritefile;
  22. char *spritename;
  23. { char buffer0[80], buffer1[80], temp;
  24.   int width, height, Row, Column, byte, i;
  25.  
  26.     if ( (fscanf(bitmapfile, "%s %s %i", buffer0, buffer1, &width) == EOF)
  27.         || (fscanf(bitmapfile, "%s %s %i", buffer0, buffer1, &height) == EOF) )
  28.     {
  29.         fprintf(stderr, "File not correct bitmap file.\n");
  30.         fclose(bitmapfile);
  31.         exit(-1);
  32.     }
  33.     do
  34.     {
  35.         fscanf(bitmapfile, "%s", buffer0);
  36.     }
  37.     while (buffer0[0] != '{');
  38.         
  39.         fprintf(spritefile,"\\sprite{\\%s}(%i,%i)[%.3fin,%.3fin]\n",
  40.             spritename, width, height,
  41.             ((width * Stuff.WidthRatio)/160.0),
  42.             ((height * Stuff.HeightRatio)/160.0) );
  43.  
  44.     for (Row = 0; Row < height; ++Row)
  45.     {
  46.         fprintf(spritefile,":");
  47.         for (Column = 0; Column < width; Column = Column + 8)
  48.         {
  49.             if (fscanf(bitmapfile, "%i%c ", &byte, &temp) == EOF)
  50.             {
  51.                 fprintf(stderr, "File not correct bitmap file.\n");
  52.                 fclose(bitmapfile);
  53.                 exit(-1);
  54.             }
  55.             for(i = 0; i<= 7; i++)
  56.             {
  57.                 if ( (byte >> i) & 1)
  58.                 {
  59.                     fprintf(spritefile,"B");
  60.                 }
  61.                 else
  62.                 {
  63.                     fprintf(spritefile,".");
  64.                 }
  65.             }
  66.         }
  67.         fprintf(spritefile," |\n");
  68.     
  69.     }
  70.     fprintf(spritefile,"\\endsprite\n");
  71.  
  72.     fclose(bitmapfile);
  73. }
  74.  
  75. void main(argc,argv)
  76. int argc; char *argv[];
  77. { ConvInfo Stuff;
  78.   FILE *bitmapfile, *spritefile;
  79.   int filestatus;
  80.   char *c;
  81.   
  82.  
  83.     /* Assign initial values to conversion information record */
  84.     Stuff.WidthRatio=INITWIDTH;
  85.     Stuff.HeightRatio=INITHEIGHT;
  86.     sprintf(Stuff.OutFileName,"\0");
  87.  
  88.     /* While there are still command line options...*/
  89.     while (--argc>0 && (*++argv)[0] == '-')
  90.     {
  91.         c = ++(*argv);
  92.  
  93.         /* User is redefining the width ratio */
  94.         if (strcmp("width",c) == 0)
  95.         {
  96.             --argc; (*++argv);
  97.             Stuff.WidthRatio= atof(*argv);
  98.         }
  99.  
  100.         /* User is redefining the height ratio */
  101.         else if (strcmp("height",c) == 0)
  102.         {
  103.             --argc; (*++argv);
  104.             Stuff.HeightRatio= atof(*argv);
  105.         }
  106.  
  107.         /* User is naming a file to put the output in */
  108.         else if (strcmp("output", c) == 0)
  109.         {
  110.             --argc; (*++argv);
  111.             strcpy(Stuff.OutFileName, *argv);
  112.         }
  113.  
  114.         /* User has given an invalid option */
  115.         else
  116.         {
  117.             fprintf(stderr,"usage:\n  bit2spr [-width width] [-height height] [-output outputfile] [bitmapfiles]\n");
  118.             exit(-1);
  119.         }
  120.     }
  121.  
  122.     /* Test to see if the user gave an output file */
  123.     if (strcmp("\0", Stuff.OutFileName) != 0)
  124.     {
  125.         /* User did, so try to open it for writing */
  126.         if ( (spritefile = fopen(Stuff.OutFileName, "wt")) == NULL)
  127.         {
  128.             /* Open failed, tell user and quit */
  129.             fprintf(stderr,"Cannot open output file %s\n",Stuff.OutFileName);
  130.             exit(-1);
  131.         }
  132.         /* Open sucess */
  133.         else fprintf(stderr,"Writing to %s\n",Stuff.OutFileName);
  134.     }
  135.  
  136.     /* User didn't supply an output file, so send output to
  137.      * standard output (usually the screen)
  138.          */
  139.     else spritefile = stdout;
  140.         
  141.  
  142.     /* Test to see if user supplied files to be read... */
  143.     if (argc == 0)
  144.         /* User didn't, so get files from stardard input */
  145.         conv_bitmap(Stuff,stdin,spritefile,"Dummy\0");
  146.  
  147.     /* User did supply files to be read, so while there are still name */
  148.     else while (--argc>=0)
  149.     {
  150.         /* ...try opening current file for reading */
  151.         if ( (bitmapfile=fopen(*argv,"r")) == NULL)
  152.             /* Error opening file for read, so tell user & continue */
  153.             fprintf(stderr,"%s doesn't exists.\n",*argv);
  154.         /* Success opening file, so convert it */
  155.         else
  156.         {
  157.             fprintf(stderr,"Converting %s...\n", *argv);
  158.             conv_bitmap(Stuff,bitmapfile,spritefile,*argv);
  159.         }
  160.  
  161.         /* move to next name */
  162.         (*++argv);
  163.     }
  164. }
  165.  
  166.